﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;

namespace MVCCalendarControlExtension
{
    public class Calendar
    {
        public string Name { get; set; }
        public DateTime SelectedDate { get; set; }
        public DateTime CalStartTime { get; set; }
        public DateTime CalEndTime { get; set; }
        public DateTime LunchStartTime { get; set; }
        public DateTime LunchEndTime { get; set; }
        public int CalSlotDuration { get; set; }
        public string ContentPath { get; set; }
        List<string> addedDateTime = new List<string>();

        Table tblCalendar = new Table();
        StringBuilder divMODetails = new StringBuilder();

        public Calendar()
        {
            SelectedDate = DateTime.Now;
            Name = "WeeklyCalendar";
        }

        public Calendar(string pName)
            : this()
        {
            Name = pName;
        }

        public Calendar(string pName, DateTime pSelectedDate)
            : this(pName)
        {
            SelectedDate = pSelectedDate;
            CalSlotDuration = 60;
            CalStartTime = DateTime.Today.AddHours(0);
            CalEndTime = DateTime.Today.AddHours(24);
        }

        //public Calendar(string pName, DateTime pSelectedDate, List<MOBookingCalendar> pBookedDates, Dictionary<string, string> pMiscInfo)
        //    : this(pName)
        //{
        //    SelectedDate = pSelectedDate;
        //    CalStartTime = DateTime.Today.AddHours(0);
        //    CalEndTime = DateTime.Today.AddHours(24);
        //    LunchStartTime = DateTime.Today.AddHours(0);
        //    LunchEndTime = DateTime.Today.AddHours(0);
        //    CalSlotDuration = 60;
        //}

        private string GenerateCalendarTable()
        {
            int num = DateTime.DaysInMonth(SelectedDate.Year, SelectedDate.Month);
            int dayOfWeek = (int)SelectedDate.DayOfWeek;
            StringBuilder builder = new StringBuilder();
            DateTime weekStartBookingDate = SelectedDate;
            DateTime midnight = new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day, 0, 0, 0);
            if (dayOfWeek > 0)
            {
                weekStartBookingDate = SelectedDate.Subtract(TimeSpan.FromDays(dayOfWeek - 1));
            }
            else
            {
                weekStartBookingDate = SelectedDate.Subtract(TimeSpan.FromDays(6));
            }

            CreateCalendarTableHeader(weekStartBookingDate);

            int rowCnt = (int)CalEndTime.Subtract(CalStartTime).TotalMinutes / CalSlotDuration;
            int cellCnt = 8;

            for (int rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
            {
                TableRow tRow = new TableRow();
                tblCalendar.Rows.Add(tRow);
                for (int cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
                {
                    TableCell tCell = new TableCell();
                    tRow.Cells.Add(tCell);
                }
            }

            int curRow = 1;
            for (int i = CalStartTime.Hour * 60; i < CalEndTime.Subtract(CalStartTime).TotalMinutes + (CalStartTime.Hour * 60); i = i + CalSlotDuration)
            {
                string currenthour = midnight.AddMinutes(i).ToShortTimeString();
                tblCalendar.Rows[curRow].Cells[0].Text = currenthour;

                for (int curCell = 1; curCell <= 7; curCell++)
                {
                    DateTime currentCellDate = weekStartBookingDate.AddDays(curCell - 1).AddMinutes(i);
                        tblCalendar.Rows[curRow].Cells[curCell].Text = "<div style='width:100%; cursor:pointer; height:20px;' class=\"basic\"  onclick=\"javascript:OpenMarriageBookingDiv('" + currentCellDate.ToString() + "');\">&nbsp;</div>";
                }
                curRow++;
            }
            DisabledPastDatesBooking(weekStartBookingDate);
            ApplyHolidaysLeavesBookingDateToCalendar(weekStartBookingDate);

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);
            htw.Flush();

            tblCalendar.RenderControl(htw);


            return sw.ToString() + divMODetails.ToString();
        }

        private void DisabledPastDatesBooking(DateTime pWeekStartBookingDate)
        {
            //int i = 0;
            //while (pWeekStartBookingDate.AddDays(i) < DateTime.Today)
            //{
            //    DateTime curDate = pWeekStartBookingDate.AddDays(i);
            //    int conCell = (int)curDate.DayOfWeek;
            //    if (conCell == 0)
            //    {
            //        conCell = 7;
            //    }
            //    int curRow = 1;
            //    if (addedDateTime.Contains(curDate.ToShortDateString()) == false)
            //    {
            //        for (int j = CalStartTime.Hour * 60; j < CalEndTime.Subtract(CalStartTime).TotalMinutes + (CalStartTime.Hour * 60); j = j + CalSlotDuration)
            //        {
            //            TableCell tc = (TableCell)tblCalendar.Controls[curRow].Controls[conCell];
            //            tc.Text = string.Empty;
            //            tc.Controls.Clear();
            //            tc.ToolTip = "Booking on past dates are not allowed";
            //            tc.CssClass = "pastdates";
            //            curRow++;
            //        }
            //        addedDateTime.Add(curDate.ToShortDateString());
            //    }
            //    i++;
            //}
        }

        private void CreateCalendarTableHeader(DateTime pWeekStartBookingDate)
        {
            tblCalendar.ID = Name;
            tblCalendar.CssClass = "tbCalendar";
            tblCalendar.Caption = SelectedDate.ToString("y");

            TableHeaderRow headerRow = new TableHeaderRow();

            TableHeaderCell headerTableCell1 = new TableHeaderCell();
            TableHeaderCell headerTableCell2 = new TableHeaderCell();
            TableHeaderCell headerTableCell3 = new TableHeaderCell();
            TableHeaderCell headerTableCell4 = new TableHeaderCell();
            TableHeaderCell headerTableCell5 = new TableHeaderCell();
            TableHeaderCell headerTableCell6 = new TableHeaderCell();
            TableHeaderCell headerTableCell7 = new TableHeaderCell();
            TableHeaderCell headerTableCell8 = new TableHeaderCell();

            headerTableCell1.Text = "Time";
            headerTableCell1.Scope = TableHeaderScope.Column;
            headerTableCell1.AbbreviatedText = "Time";
            headerTableCell2.Text = string.Format("{0}&nbsp;&nbsp;Monday", pWeekStartBookingDate.AddDays(0).Day);
            headerTableCell2.Scope = TableHeaderScope.Column;
            headerTableCell2.AbbreviatedText = "Monday";
            headerTableCell3.Text = string.Format("{0}&nbsp;&nbsp;Tuesday", pWeekStartBookingDate.AddDays(1).Day);
            headerTableCell3.Scope = TableHeaderScope.Column;
            headerTableCell3.AbbreviatedText = "Tuesday";
            headerTableCell4.Text = string.Format("{0}&nbsp;&nbsp;Wednesday", pWeekStartBookingDate.AddDays(2).Day);
            headerTableCell4.Scope = TableHeaderScope.Column;
            headerTableCell4.AbbreviatedText = "Wednesday";
            headerTableCell5.Text = string.Format("{0}&nbsp;&nbsp;Thursday", pWeekStartBookingDate.AddDays(3).Day);
            headerTableCell5.Scope = TableHeaderScope.Column;
            headerTableCell5.AbbreviatedText = "Thursday";
            headerTableCell6.Text = string.Format("{0}&nbsp;&nbsp;Friday", pWeekStartBookingDate.AddDays(4).Day);
            headerTableCell6.Scope = TableHeaderScope.Column;
            headerTableCell6.AbbreviatedText = "Friday";
            headerTableCell7.Text = string.Format("{0}&nbsp;&nbsp;Saturday", pWeekStartBookingDate.AddDays(5).Day);
            headerTableCell7.Scope = TableHeaderScope.Column;
            headerTableCell7.AbbreviatedText = "Saturday";
            headerTableCell8.Text = string.Format("{0}&nbsp;&nbsp;Sunday", pWeekStartBookingDate.AddDays(6).Day);
            headerTableCell8.Scope = TableHeaderScope.Column;
            headerTableCell8.AbbreviatedText = "Sunday";

            headerRow.Cells.Add(headerTableCell1);
            headerRow.Cells.Add(headerTableCell2);
            headerRow.Cells.Add(headerTableCell3);
            headerRow.Cells.Add(headerTableCell4);
            headerRow.Cells.Add(headerTableCell5);
            headerRow.Cells.Add(headerTableCell6);
            headerRow.Cells.Add(headerTableCell7);
            headerRow.Cells.Add(headerTableCell8);


            tblCalendar.Rows.AddAt(0, headerRow);
        }

        private void ApplyHolidaysLeavesBookingDateToCalendar(DateTime pWeekStartBookingDate)
        {
            //foreach (MOBookingCalendar bevent in BookedDates)
            //{
            //    MOBookingType MOType = (MOBookingType)Enum.Parse(typeof(MOBookingType), bevent.TypeOfBooking);
            //    if (bevent.StartDateTime < pWeekStartBookingDate)
            //    {
            //        bevent.StartDateTime = pWeekStartBookingDate;
            //    }
            //    if (bevent.EndDateTime > pWeekStartBookingDate.AddDays(6) )
            //    {
            //        bevent.EndDateTime = pWeekStartBookingDate.AddDays(6);
            //    }
            //    switch (MOType)
            //    {
            //        case MOBookingType.Holiday:
            //            int i = 0;
            //            while (bevent.StartDateTime.AddDays(i) <= bevent.EndDateTime && i < 7)
            //            {
            //                DateTime curDate = bevent.StartDateTime.AddDays(i);
            //                int conCell = (int)curDate.DayOfWeek ;
            //                if (conCell == 0)
            //                {
            //                    conCell = 7;
            //                }
            //                int curRow = 1;
            //                if (addedDateTime.Contains(curDate.ToShortDateString()) == false)
            //                {
            //                    for (int j = CalStartTime.Hour * 60; j < CalEndTime.Subtract(CalStartTime).TotalMinutes + (CalStartTime.Hour * 60); j = j + CalSlotDuration)
            //                    {
            //                        TableCell tc = (TableCell)tblCalendar.Controls[curRow].Controls[conCell];
            //                        tc.Text = string.Empty;
            //                        tc.Controls.Clear();
            //                        tc.ToolTip = bevent.MarriageDTReference;
            //                        tc.CssClass = "holiday";
            //                        curRow++;
            //                    }
            //                    addedDateTime.Add(curDate.ToShortDateString());
            //                }
            //                i++;
            //            }
            //            break;
            //        case MOBookingType.Leave:
            //            int m = 0;
            //            while (bevent.StartDateTime.AddDays(m) <= bevent.EndDateTime)
            //            {
            //                DateTime curDate = bevent.StartDateTime.AddDays(m);
            //                int conCell = (int)curDate.DayOfWeek ;
            //                if (conCell == 0)
            //                {
            //                    conCell = 7;
            //                }
            //                int curRow = 1;
            //                if (addedDateTime.Contains(curDate.ToShortDateString()) == false)
            //                {
            //                    for (int j = CalStartTime.Hour * 60; j < CalEndTime.Subtract(CalStartTime).TotalMinutes + (CalStartTime.Hour * 60); j = j + CalSlotDuration)
            //                    {
            //                        TableCell tc = (TableCell)tblCalendar.Controls[curRow].Controls[conCell];
            //                        tc.Text = string.Empty;
            //                        tc.Controls.Clear();
            //                        tc.ToolTip = bevent.MarriageDTReference;
            //                        tc.CssClass = "leave";
            //                        curRow++;
            //                    }
            //                    addedDateTime.Add(curDate.ToShortDateString());
            //                }
            //                m++;
            //            }
            //            break;
            //        case MOBookingType.Booking:
            //            string divName = "divMODetails" + Guid.NewGuid();
            //            string divContent = GetDivContent(bevent, BookedDates);
            //            string viewBookedMO = "<a style=\"font-family:tahoma;font-size:10px\" onmouseover=\"ShowMBDetailsOfAllOfficers('" + divName + "'); return true;\" onmouseout=\"HideMBDetailsOfAllOfficers('" + divName + "'); return true;\" href=\"javascript:void(0)\">view details</a>";
            //            divMODetails.AppendFormat("<div id=\"{0}\" style=\"display: none; position: absolute; border-style: solid; background-color: white; padding: 5px;\">{1}</div>", divName, divContent);

            //            DateTime curBDate = bevent.StartDateTime;
            //            int conBCell = (int)curBDate.DayOfWeek;
            //            if (conBCell == 0)
            //            {
            //                conBCell = 7;
            //            }
            //            int curBRow = (((bevent.StartDateTime.Hour * 60) + bevent.StartDateTime.Minute) - ((CalStartTime.Hour * 60) + CalStartTime.Minute)) / CalSlotDuration;
            //            for (int j = 0; j < bevent.SlotDuration; j++)
            //            {
            //                TableCell tc = null;
            //                try
            //                {
            //                    tc = (TableCell)tblCalendar.Controls[curBRow + 1].Controls[conBCell];
            //                }
            //                catch
            //                { }
            //                if (tc != null)
            //                {
            //                    tc.Controls.Clear();
            //                    tc.CssClass = "booked";
            //                    tc.Text = viewBookedMO;
            //                }

            //                curBRow++;
            //            }
            //            break;
            //        default:
            //            break;
            //    }
            //}
        }

        //private string GetDivContent(MOBookingCalendar pBookingEvent, List<MOBookingCalendar> pBookedDates)
        //{
        //    //List<MOBookingCalendar> bEvents = pBookedDates.FindAll(p => p.StartDateTime == pBookingEvent.StartDateTime);

        //    //StringBuilder officerName = new StringBuilder();
        //    //foreach (MOBookingCalendar bevent in bEvents)
        //    //{
        //    //    officerName.Append(bevent.BookingOfficer + "<br>");
        //    //}
        //    //return officerName.ToString();
        //    return string.Empty;  

        //}

        public override string ToString()
        {
            return GenerateCalendarTable();
        }
    }
}
